package com.contextor.magiccards.web.to.battle;

import com.contextor.magiccards.model.battle.Battle;
import com.contextor.magiccards.model.cards.MagicSchool;
import com.contextor.magiccards.model.character.Personage;
import com.contextor.magiccards.model.user.User;
import com.contextor.magiccards.web.to.BaseTO;
import com.contextor.magiccards.web.to.user.PersonageTO;

import java.util.Date;


/**
 * User: sergey
 * Date: Mar 22, 2011 11:56:30 AM
 */
public class BattleTO extends BaseTO {
    public Integer version;
    public Integer moveDuration;
    public Integer level;
    public PersonageTO firstPlayer;
    public PersonageTO secondPlayer;
    public Date dateCreated;

    public BattleTO() {
    }

    public BattleTO(Battle battle) {
        this.id = battle.getId();
        this.userId = battle.getUid();
        this.version = battle.getVersion();
        this.moveDuration = battle.getMaxMoveDuration();
        this.level = battle.getLevel();
        this.dateCreated = battle.getDateCreated();
        if (battle.getFirstPlayer() != null) {
            this.firstPlayer = new PersonageTO(battle.getFirstPlayer(), battle.getUid());
        }
        if (battle.getSecondPlayer() != null) {
            this.secondPlayer = new PersonageTO(battle.getSecondPlayer(), battle.getUid());
        }
    }

    public Battle toModel() {
        final Battle battle = new Battle();
        battle.setId(this.id);
        battle.setUser(new User(this.userId));
        battle.setVersion(this.version);
        battle.setMaxMoveDuration(this.moveDuration);
        battle.setLevel(this.level);
        battle.setDateCreated(this.dateCreated);
        if (this.firstPlayer != null) {
            final Personage personage = new Personage();
            personage.setId(firstPlayer.id);
            personage.setLevel(firstPlayer.level);
            personage.setName(firstPlayer.name);
            personage.setMagicSchool(MagicSchool.valueOf(firstPlayer.magicSchool));
            battle.setFirstPlayer(personage);
        }
        if (this.secondPlayer != null) {
            final Personage personage = new Personage();
            personage.setId(secondPlayer.id);
            personage.setLevel(secondPlayer.level);
            personage.setName(secondPlayer.name);
            personage.setMagicSchool(MagicSchool.valueOf(secondPlayer.magicSchool));
            battle.setSecondPlayer(personage);
        }
        return battle;
    }
}
